/*this module is code by erek 2009-4-1*/
#include "zdfl_parameter_define.h"
#include "User_handle.h"
#include "md_OSD.h"
#include "app_OSD.h"
#include <stdarg.h>
#include "Printf.h"
#if 0
#ifdef Update_AvoidBlink
U8 UpdateStatus=0;
#endif
#endif

extern U8  OSDLang; //this variable is used for setting muti-language

extern BYTE Use_UnicodeFont;

void TWLL_OSDLayer_Init(void)
{
	MD_OSDLayer_Init();
}

void TWLL_WaitOSDBlank(U8 Cnt)
{
	MD_WaitOSDBlank(Cnt);
}

void TWLL_WaitDisplayBlank(U8 Cnt)
{
	MD_WaitDisplayBlank(Cnt);
}

#ifdef Used_BlockTransfer 
void TWLL_BlockTransfer(U16 sx, U16 sy, U16 w, U16 h, U16 dx, U16 dy,U8 DispMode)reentrant
{
	//Printf("\nsx=%d,sy=%d,w=%d,h=%d,dx=%d,dy=%d,DispMode=%bd\r\n",sx,sy,w,h,dx,dy,DispMode);
	MD_BlockTransfer(sx,sy,w,h,dx,dy,DispMode);
}
#endif

#if 0
#ifdef Update_AvoidBlink 
void TWLL_UpdateButton_WithBT(U16 Ctrl)
{
	LPUIPBUTTON  lpBtn;

	lpBtn=(LPUIPBUTTON )GetCtrlHandle(Ctrl);

	UpdateStatus=1;
	ZUIRedrawCtrl(Ctrl);
	
	TWLL_WaitOSDBlank(1);
	TWLL_BlockTransfer((User_Canvas[IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].Canvas_ID].Mem_X+IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].DispControl->XY1.X+800),(User_Canvas[IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].Canvas_ID].Mem_Y+IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].DispControl->XY1.Y),SystemIcon[IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].BMP_ID].width,SystemIcon[IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].BMP_ID].height,(User_Canvas[IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].Canvas_ID].Mem_X+IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].DispControl->XY1.X),(User_Canvas[IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].Canvas_ID].Mem_Y+IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].DispControl->XY1.Y),User_Canvas[IconShowOnCanvas[lpBtn->hImageID[ZCTRL_UNFOCUS]].Canvas_ID].Bits_Per_Pixel);
	UpdateStatus=0;
}

void TWLL_UpdateProgressBar_WithBT(U16 Ctrl)
{
	LPUIPROGRESS  lpPro;

	lpPro=(LPUIPROGRESS )GetCtrlHandle(Ctrl);

	UpdateStatus=1;
	ZUIRedrawCtrl(Ctrl);
	
	TWLL_WaitOSDBlank(1);
	TWLL_BlockTransfer((User_Canvas[IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].Canvas_ID].Mem_X+IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].DispControl->XY1.X+800),(User_Canvas[IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].Canvas_ID].Mem_Y+IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].DispControl->XY1.Y),SystemIcon[IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].BMP_ID].width,SystemIcon[IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].BMP_ID].height,(User_Canvas[IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].Canvas_ID].Mem_X+IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].DispControl->XY1.X),(User_Canvas[IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].Canvas_ID].Mem_Y+IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].DispControl->XY1.Y),User_Canvas[IconShowOnCanvas[lpPro->hImage[ZPRO_IMAGE_BACKGROUND]].Canvas_ID].Bits_Per_Pixel);
	UpdateStatus=0;
}
#endif
#endif

void TWLL_SetWin_GlobalAlpha(U8 WinNO, U8 alpha_level)
{
	MD_SetWin_GlobalAlpha(WinNO,alpha_level);
}

/******************************************************************************
 Initial the Canvas
 
Parameters
pCanvas using to control the attribute of the canvas, including the window's memory start address, 
screen address, width and height etc.
*******************************************************************************/
void TWLL_Canvas_Init(CANVAS* pCanvas)
{
	U8 WinNO;
	U16 HStart, HLength,VStart,VLength,xp,yp;
	U8 i,j;
	U16 *alpha_color;
	U8 color_Num,alpha_mode,alpha_level;

	WinNO=pCanvas->Display_Layer;
	xp=pCanvas->Mem_X;
	yp=pCanvas->Mem_Y;

	HStart=pCanvas->Screen_X;
	HLength=pCanvas->width;
	VStart=pCanvas->Screen_Y;
	VLength=pCanvas->height;

	//MD_SetAlphaBlending_Disable(WinNO);  //for demo 20100414

	//Canvas Attribute
	if(pCanvas->attribute)
	{
		for(i=0;i<4;i++)
		{
			if(pCanvas->attribute[i].attri_kind==0xff) break;

			//Canvas Attribute, here is alpha blending
			if(pCanvas->attribute[i].attri_kind==0x01)
			{
				if(pCanvas->attribute[i].mode==0x01) //Global window alpha
				{
					alpha_level=pCanvas->attribute[i].Level;
					MD_SetWin_GlobalAlpha(WinNO,alpha_level);
				}
				else if(pCanvas->attribute[i].mode==0x02)
				{
					if(WinNO==0||WinNO==1)
						MD_SetWin_PerPixelAlpha(WinNO);
					else
					{
						//for(j=0;j<pCanvas->attribute[i].color_number;j++)
						//{
						alpha_color=pCanvas->attribute[i].Color;
						color_Num=pCanvas->attribute[i].color_number;
						alpha_level=pCanvas->attribute[i].Level;
						MD_SetWin_ColorKeyAlpha(color_Num,alpha_color,alpha_level);
						//}
					}
				}
			}
		}
	}
	
	//MD_SetWin_ScaleRatio(WinNO,0,0,0,0);
	MD_SetWin_MemoryPosition(WinNO,xp,yp);
	MD_SetWin_Infomation(WinNO,HStart,HLength,VStart,VLength);
}


/******************************************************************************
 Control the Canvas On or Off
 
Parameters
CanNO is canvas's number
OnOff is a static symbol which indicate the canvas whether on or off
*******************************************************************************/
void TWLL_Canvas_OnOff(U8 CanNO, U8 OnOff )
{
	MD_Win_OnOff(CanNO,OnOff);
}

#ifndef DP80390
void TWLL_Load_AllScratch_page(void)reentrant
{
	U8 i;
	for(i=0;i<INIT_LOAD_SCRATCH_PAGE;i++)
	MD_Load_Scratch_page(i);
}

void TWLL_Load_PerScratch_page( U8 ScratchNO)reentrant
{
	MD_Load_Scratch_page(ScratchNO);
}
#endif

/******************************************************************************
 Set Canvas Pallette
 
Parameters
canvas_id is canvas's number
pal_id is pallette number which will be use by canvas
*******************************************************************************/
void TWLL_Canvas_Pallette_Set(U8 canvas_id, U8 pal_id)
{
	MD_Win_Pallette_Set(canvas_id,pal_id);
}


/******************************************************************************
The following functions will be called automatically, when the user menu page is created. The
objects within the current menu page will be created and the necessary hardware resource has to
be allocated.
*******************************************************************************/
void TWLL_Icon_Create(U16 UICtrlID, UIControlIcon_struct * UIIcon)
{	
	U8 convert_Num,overwrite_Num, DispMode;
	U16 *convert_color,*overwrite_color;
	U8 i;

	DispMode=User_Canvas[UIIcon[UICtrlID].Canvas_ID].Bits_Per_Pixel;

	//Printf("\r\n Stone TWLL_Icon_Create %bd", UIIcon[UICtrlID].Canvas_ID);
	//Icon Attribute
	if(UIIcon[UICtrlID].DispControl->attribute)
	{
		for(i=0;i<4;i++)
		{
			if(UIIcon[UICtrlID].DispControl->attribute[i].attri_kind==0xff) break;

			//Icon Attribute, here is convert
			if(UIIcon[UICtrlID].DispControl->attribute[i].attri_kind==0x00)
			{	
				convert_Num=UIIcon[UICtrlID].DispControl->attribute[i].color_number;
				convert_color=UIIcon[UICtrlID].DispControl->attribute[i].Color;
				MD_SetCtrl_ConvertColor(convert_Num,convert_color,DispMode);
			}

			//Icon Attribute, here is overwrite
			if(UIIcon[UICtrlID].DispControl->attribute[i].attri_kind==0x03)
			{
				overwrite_Num=UIIcon[UICtrlID].DispControl->attribute[i].color_number;
				overwrite_color=UIIcon[UICtrlID].DispControl->attribute[i].Color;
				MD_SetCtrl_OverWriteColor(overwrite_Num,overwrite_color,DispMode);
			}
		}
	}

	//Printf("\ntest\r\n");
	MD_BMP_Display(&UIIcon[UICtrlID]);

	MD_SetColorConvert_Disable();
	MD_SetSelectiveOvWrite_Disable();
}

void TWLL_String_Create(U16 UICtrlID, UIControlString_struct *UIString)
{
	U8 overwrite_Num,DispMode;
	U16 *overwrite_color;
	U16 convert_color[4];
	U8 i;
	
	UICtrlID +=0;

	DispMode=User_Canvas[UIString[UICtrlID].Canvas_ID].Bits_Per_Pixel;
	
	//String color
	if(DispMode==16)
	{
		convert_color[0]=0x001f;
		convert_color[1]=*((U16*)UIString[UICtrlID].DispControl->FontColor->Font_Color_table);
		convert_color[2]=0xfffe;
		convert_color[3]=*((U16*)UIString[UICtrlID].DispControl->FontColor->Font_Color_table+1);
	}
	else if(DispMode==8)
	{
		convert_color[0]=0x00;
		convert_color[1]=*((U8*)UIString[UICtrlID].DispControl->FontColor->Font_Color_table);
		convert_color[2]=0x01;
		convert_color[3]=*((U8*)UIString[UICtrlID].DispControl->FontColor->Font_Color_table+1);
		//Printf("\r\n convert_color[0]=%x, convert_color[1]=%x,convert_color[2]=%x,convert_color[3]=%x\r\n",convert_color[0],convert_color[1],convert_color[2],convert_color[3]);
	}
	MD_SetCtrl_ConvertColor(4,convert_color,DispMode);

	//String Attribute
	if(UIString[UICtrlID].DispControl->attribute)
	{
		for(i=0;i<4;i++)
		{
			if(UIString[UICtrlID].DispControl->attribute[i].attri_kind==0xff) break;

			//String Attribute, here is overwrite
			if(UIString[UICtrlID].DispControl->attribute[i].attri_kind==0x03)
			{
				overwrite_Num=UIString[UICtrlID].DispControl->attribute[i].color_number;
				overwrite_color=UIString[UICtrlID].DispControl->attribute[i].Color;
				MD_SetCtrl_OverWriteColor(overwrite_Num,overwrite_color,DispMode);
			}
		}
	}
	
	MD_String_Display(&UIString[UICtrlID]);

	MD_SetColorConvert_Disable();
	MD_SetSelectiveOvWrite_Disable();
}

void TWLL_Rect_Create(U16 UICtrlID, UIControlRect_struct *UIRect)
{	
	MD_Rect_Draw(UIRect[UICtrlID].Canvas_ID,DISPLAY_BUFFER, UIRect[UICtrlID].DispControl);	
}

#ifndef DP80390
void TWLL_Line_Create(U16 UICtrlID, UIControlLine_struct * UILine)reentrant
{	
	MD_Line_Draw(UILine[UICtrlID].Canvas_ID, DISPLAY_BUFFER, UILine[UICtrlID].DispControl);	
}
#endif

/******************************************************************************
The following functions will be called automatically, when part of whole user menu page need be
updated. If the objects within the current menu page have the attribute change, they need be
redraw as well.
*******************************************************************************/
#ifndef DP80390
void TWLL_Icon_Redraw(U16 UICtrlID, UIControlIcon_struct * UIIcon)reentrant
{	
	U8 convert_Num,overwrite_Num,DispMode;
	U16 *convert_color,*overwrite_color;
	U8 i;

	DispMode=User_Canvas[UIIcon[UICtrlID].Canvas_ID].Bits_Per_Pixel;

	//Icon Attribute
	if(UIIcon[UICtrlID].DispControl->attribute)
	{
		for(i=0;i<4;i++)
		{
			if(UIIcon[UICtrlID].DispControl->attribute[i].attri_kind==0xff) break;

			//Icon Attribute, here is convert
			if(UIIcon[UICtrlID].DispControl->attribute[i].attri_kind==0x00)
			{	
				convert_Num=UIIcon[UICtrlID].DispControl->attribute[i].color_number;
				convert_color=UIIcon[UICtrlID].DispControl->attribute[i].Color;
				MD_SetCtrl_ConvertColor(convert_Num,convert_color,DispMode);
			}

			//Icon Attribute, here is overwrite
			if(UIIcon[UICtrlID].DispControl->attribute[i].attri_kind==0x03)
			{
				overwrite_Num=UIIcon[UICtrlID].DispControl->attribute[i].color_number;
				overwrite_color=UIIcon[UICtrlID].DispControl->attribute[i].Color;
				MD_SetCtrl_OverWriteColor(overwrite_Num,overwrite_color,DispMode);
			}
		}
	}
	
	MD_BMP_Display(&UIIcon[UICtrlID]);

	MD_SetColorConvert_Disable();
	MD_SetSelectiveOvWrite_Disable();
}

void TWLL_String_Redraw(U16 UICtrlID, UIControlString_struct *UIString)reentrant
{
	U8 overwrite_Num,DispMode;
	U16 *overwrite_color;
	U16 convert_color[4];
	U8 i;
	
	UICtrlID +=0;
	
	DispMode=User_Canvas[UIString[UICtrlID].Canvas_ID].Bits_Per_Pixel;
	
	//String color
	if(DispMode==16)
	{
		convert_color[0]=0x001f;
		convert_color[1]=*((U16*)UIString[UICtrlID].DispControl->FontColor->Font_Color_table);
		convert_color[2]=0xfffe;
		convert_color[3]=*((U16*)UIString[UICtrlID].DispControl->FontColor->Font_Color_table+1);
	}
	else if(DispMode==8)
	{
		convert_color[0]=0x00;
		convert_color[1]=*((U8*)UIString[UICtrlID].DispControl->FontColor->Font_Color_table);
		convert_color[2]=0x01;
		convert_color[3]=*((U8*)UIString[UICtrlID].DispControl->FontColor->Font_Color_table+1);
	}
	MD_SetCtrl_ConvertColor(4,convert_color,DispMode);

	//String Attribute
	if(UIString[UICtrlID].DispControl->attribute)
	{
		for(i=0;i<4;i++)
		{
			if(UIString[UICtrlID].DispControl->attribute[i].attri_kind==0xff) break;

			//String Attribute, here is overwrite
			if(UIString[UICtrlID].DispControl->attribute[i].attri_kind==0x03)
			{
				overwrite_Num=UIString[UICtrlID].DispControl->attribute[i].color_number;
				overwrite_color=UIString[UICtrlID].DispControl->attribute[i].Color;
				MD_SetCtrl_OverWriteColor(overwrite_Num,overwrite_color,DispMode);
			}
		}
	}
	
	MD_String_Display(&UIString[UICtrlID]);

	MD_SetColorConvert_Disable();
	MD_SetSelectiveOvWrite_Disable();
}

void TWLL_Rect_Redraw(U16 UICtrlID, UIControlRect_struct *UIRect)reentrant
{
	MD_Rect_Draw(UIRect[UICtrlID].Canvas_ID,DISPLAY_BUFFER, UIRect[UICtrlID].DispControl);	
}

void TWLL_Line_Redraw(U16 UICtrlID, UIControlLine_struct * UILine)reentrant
{
	MD_Line_Draw(UILine[UICtrlID].Canvas_ID, DISPLAY_BUFFER, UILine[UICtrlID].DispControl);	
}
#endif
/******************************************************************************
The following functions will be called automatically, when the user menu page exiting. 
The objects within the current menu page will be erased and the occupied hardware resource need be released
*******************************************************************************/
void TWLL_Icon_Remove(U16 UICtrlID, UIControlIcon_struct * UIIcon)
{
	RectangleControl_struct Cntr;
	U8 UserCanvasID = UIIcon->Canvas_ID;
	U16 width, height;

    	MD_Get_Icon_size(UIIcon->BMP_ID, &width, &height );

	Cntr.XY1.X = UIIcon->DispControl->XY1.X;
	Cntr.XY1.Y = UIIcon->DispControl->XY1.Y;
	Cntr.XY2.X = UIIcon->DispControl->XY1.X + width ;
	Cntr.XY2.Y = UIIcon->DispControl->XY1.Y + height;
	Cntr.Alpha = 0;
	Cntr.Attribute = 0;

	Cntr.Color = 0;//TWLL_Get_Canvas_Color(UIIcon->Canvas_ID);

    	//MD_Rect_Draw(UserCanvasID,DISPLAY_BUFFER, &Cntr);

}

void TWLL_String_Remove(U16 UICtrlID, UIControlString_struct *UIString)
{
	RectangleControl_struct Cntr;
	U8 UserCanvasID = UIString->Canvas_ID;
	U16 width, height;

	MD_Get_String_Size(UIString, &width, &height);

	Cntr.XY1.X = UIString->DispControl->XY1.X;
	Cntr.XY1.Y = UIString->DispControl->XY1.Y;
	Cntr.XY2.X = UIString->DispControl->XY1.X + width ;
	Cntr.XY2.Y = UIString->DispControl->XY1.Y + height;
	Cntr.Alpha = 0;
	Cntr.Attribute = 0;

	Cntr.Color = 0;//TWLL_Get_Canvas_Color(UIString->Canvas_ID);

    	MD_Rect_Draw(UserCanvasID,DISPLAY_BUFFER, &Cntr);
}

#ifndef DP80390
void TWLL_Rect_Remove(U16 UICtrlID, UIControlRect_struct *UIRect)reentrant
{
	 RectangleControl_struct Cntr;
	U8 UserCanvasID = UIRect->Canvas_ID;

	Cntr.XY1.X = UIRect->DispControl->XY1.X;
	Cntr.XY1.Y = UIRect->DispControl->XY1.Y;
	Cntr.XY2.X = UIRect->DispControl->XY2.X;
	Cntr.XY2.Y = UIRect->DispControl->XY2.Y;
	Cntr.Alpha = 0;
	Cntr.Attribute = 0;

	Cntr.Color = 0;//TWLL_Get_Canvas_Color(UIString->Canvas_ID);

    	MD_Rect_Draw(UserCanvasID,DISPLAY_BUFFER, &Cntr);
}

void TWLL_Line_Remove(U16 UICtrlID, UIControlLine_struct * UILine)reentrant
{
	 LineControl_struct Cntr;
	U8 UserCanvasID = UILine->Canvas_ID;

	Cntr.XY1.X = UILine->DispControl->XY1.X;
	Cntr.XY1.Y = UILine->DispControl->XY1.Y;
	Cntr.XY2.X = UILine->DispControl->XY2.X;
	Cntr.XY2.Y = UILine->DispControl->XY2.Y;
	Cntr.Width=UILine->DispControl->Width;
	Cntr.Alpha = 0;
	Cntr.Attribute = 0;

	Cntr.Color = 0;//TWLL_Get_Canvas_Color(UIString->Canvas_ID);

	MD_Line_Draw(UserCanvasID,DISPLAY_BUFFER, &Cntr);	

}
#endif 

void TWLL_Get_CharSize(U16 Font_index, U8 *width, U8* height)reentrant
{
	MD_Get_FONT_Size(Font_index, width, height);
}

void TWLL_Get_TextSize(UIControlString_struct *UIString, U16 *width, U16 *height)reentrant
{
	MD_Get_String_Size(UIString,width,height);
}

U16 TWLL_Get_ImageID(U16 UICtrlID, UIControlIcon_struct * UIIcon)reentrant
{
	return UIIcon[UICtrlID].BMP_ID;
}


void TWLL_Get_ImageSize(U16 imageid, U16 * pw, U16 *ph)reentrant
{
	MD_Get_Icon_size(imageid,pw,ph);
}

U8 TWLL_Get_ImageCanvas(U16 UICtrlID, UIControlIcon_struct * UIIcon)reentrant//erek 2011-1-26 for bank overflow issue
{
	return UIIcon[UICtrlID].Canvas_ID;
}

void TWLL_Get_IconShowOnCanvasXY(U16 UICtrlID, UIControlIcon_struct * UIIcon, U16 * X, U16 *Y)reentrant//erek 2011-1-26 for bank overflow issue
{
	*X=UIIcon[UICtrlID].DispControl->XY1.X;
	*Y=UIIcon[UICtrlID].DispControl->XY1.Y;
}

void Draw_DialString(U16 Number,U8 Count,U16 Value)  //erek 2011-1-26 for bank overflow issue
{
	StringShowOnCanvas[Number].Character[OSDLang].Str_Lang[Count]=Value;

}

void ZApp_Draw_Image_XY(U16 UICtrlID, UIControlIcon_struct * UIIcon, int x, int y)//erek 2011-1-26 for bank overflow issue
{
    UIControlIcon_struct icon_show[1];
    IconControl_struct display_control;

    icon_show[0]=UIIcon[UICtrlID];
    display_control.attribute= UIIcon[UICtrlID].DispControl->attribute;
    display_control.XY1.X = x;
    display_control.XY1.Y = y;
    icon_show[0].DispControl=&display_control;
    TWLL_Icon_Create(0, icon_show);
}

void ZApp_Display_Image_XY(U16 bitmap_id, U8 canvas_id, U16 px, U16 py)//erek 2011-1-26 for bank overflow issue
{
    UIControlIcon_struct icon_show[1];
    IconControl_struct dpc;
    icon_show[0].DispControl = &dpc;
    dpc.XY1.X = px;
    dpc.XY1.Y = py;

    icon_show[0].Canvas_ID = canvas_id;
    icon_show[0].BMP_ID = bitmap_id;

    TWLL_Icon_Create(0, icon_show);
}




////////////////////ZPrintf///////////////////////////////////
//BYTE zp_str[101];
BYTE zp_str[40]; //erek for TW8823C 20121120

int zp_len;

int zp_x, zp_y;

/*
typedef char *va_list;

#define va_start(ap, p)	(ap = (char *) (&(p)+1))
#define va_arg(ap, type)	((type *) (ap += sizeof(type)))[-1]
#define va_end(ap)		((void)0)
*/

void outchar(char c)
{
	BYTE b = (char) c;
	
	if(zp_len < 100)
		zp_str[zp_len ++] = b;
}

void DoZPrint(const char *fmt, va_list ap )
{
	
	const U8 Hex[] = "0123456789ABCDEF";
	
	char  ch;
	char  i;
	long  value=0;
	unsigned char  fl_zero;
	unsigned char  fl_num;
	unsigned char  fl_len;
	unsigned char  cnt;
	unsigned long int mask=1;
	char *ptr;
	
	zp_len = 0;
	
	while(1)
	{
		//----- Find Formatter % -----
		switch( ch = *fmt++ )
		{
			case 0:
				return;
			case '%':
				if( *fmt != '%' )
					break;
				fmt++;
			default:
				outchar( ch );
				continue;
		}
		//----- Get Count -------------
		fl_zero = 0;
		fl_num = 0;
		cnt = 0;
		ch = *fmt++;
		if( ch=='0' )
		{
			fl_zero = 1;
			ch = *fmt++;
			cnt = (U8)(ch - '0');
			ch = *fmt++;
		}
		else if( ch>='0' && ch<='9' )
		{
			cnt = (U8)(ch - '0');
			ch = *fmt++;
		}
		//----- Get char(B) / int / long(L) ----------------
		fl_len = 2;
		switch(ch)
		{
			case 'l':
			case 'L':
				ch = *fmt++;
				fl_len = 4;
				break;
			case 'b':
			case 'B':
				ch = *fmt++;
				fl_len = 1;
				break;
		}
		//----- Get Type Discriptor -----
		switch( ch )
		{
			case 'd':
			case 'u':
				switch (fl_len)
				{
					case 1:
						if( ch=='d' )
							value = (char)va_arg( ap, char );
						else
							value = (unsigned char)va_arg( ap, unsigned char );
						break;
					case 2:
						if( ch=='d' )
							value = (int)va_arg( ap,  int );
						else
							value = (unsigned int)va_arg( ap, unsigned int );
						break;
					case 4:
						if( ch=='d' )
							value = (long)va_arg( ap, long );
						else
							value = (unsigned long int)va_arg( ap, unsigned long int );
						break;
				}
				if( value<0 )
				{
					outchar('-');
					value = value*(-1);
				}
				if(cnt==0)
				{
					if( value==0 )
					{
						outchar('0');
						continue;
					}
					for(cnt=0, mask=1; cnt<10; cnt++)
					{
						if( (value/mask)==0 )
							break;
						mask = mask*10;
					}
				}
				for(i=0, mask=1; i<cnt-1; i++)
					mask = mask*10;
				while(1)
				{
					ch = (char)((value / mask) + '0');
					if( ch=='0' && fl_zero==0 && mask!=1 )
						ch=' ';
					else
						fl_zero = 1;
					outchar(ch);
					value = value % (mask);
					mask = mask / 10;
					if( mask==0 )
						break;
				}
				continue;
			case 'x':
			case 'X':
				switch (fl_len)
				{
					case 1:
						value = (unsigned char)va_arg( ap, unsigned char );
						break;
					case 2:
						value = (unsigned int)va_arg( ap, unsigned int );
						break;
					case 4:
						value = (unsigned long int)va_arg( ap, unsigned long int );
						break;
				}
				if(cnt==0)
					cnt = (U8)(fl_len*2);
				for(i=0; i<cnt; i++)
				{
					outchar(Hex[(U16)((value>>(U8)((cnt-i-1)*4)))&(U16)0x000f]);//Hex[((U32)value>>(U8)((cnt-(U8)i-1)*4))&(U32)0x000f]
				}
				continue;
			case 's':
				//#ifdef KEILC
				ptr = (char *)va_arg( ap, char* );
				while(*ptr!='\0')
					outchar(*ptr++);
				continue;
				//#endif
			case 'c':
				value = va_arg( ap, int );
				outchar((unsigned char)value);
				continue;
			default:
				value = (unsigned int)va_arg( ap, int );
				continue;
		}
	}	
}

void ZPrintf_XY(U16 x, U16 y, U16 B_Color, U16 F_Color,U8 Canvas_ID, const char *fmt, ... )
{
	U16 i;
	
	U16 StringColorTab[2]={0x0000,0xfffe};

	FONTCOLOR StringColor[]={
		StringColorTab,
		2,
	};
	
	FontControl_struct zp_control[]={
		{{120,20},0,StringColor},
	};
	
	U16 zp_data[64];

	CONST Character_ptr String_character[]={
	{zp_data },
	{zp_data },
	{zp_data },
	{zp_data },
	{zp_data },
	};
	
	UIControlString_struct zp[] = {{0, zp_control ,5, String_character,0, 0}};
	
	va_list ap;
	
	zp_control[0].XY1.X = x;
	zp_control[0].XY1.Y = y;

	StringColorTab[0]=B_Color;
	StringColorTab[1]=F_Color;
	
	zp->Canvas_ID=Canvas_ID;
	
	va_start(ap, fmt);
	DoZPrint( fmt, ap );
	va_end( ap );	
	
	///////str in zp_str, zp_len
	for(i=0; i<zp_len; i++){
		zp_data[i] = zp_str[i];
	}
	
	zp_data [zp_len]=0;
	
	TWLL_String_Create(0,zp);
	
}

void ZPrintf_XY_8Bit(U16 x, U16 y, U8 B_Color, U8 F_Color,U8 Canvas_ID, const char *fmt, ... )
{
	U16 i;
	
	U8 StringColorTab[2]={0x00,0x01};

	FONTCOLOR StringColor[]={
		StringColorTab,
		2,
	};
	
	FontControl_struct zp_control[]={
		{{120,20},0,StringColor},
	};
	
	U16 zp_data[64];

	CONST Character_ptr String_character[]={
	{zp_data },
	{zp_data },
	{zp_data },
	{zp_data },
	{zp_data },
	};
	
	UIControlString_struct zp[] = {{0, zp_control ,5, String_character,0, 0}};
	
	va_list ap;
	
	zp_control[0].XY1.X = x;
	zp_control[0].XY1.Y = y;

	StringColorTab[0]=B_Color;
	StringColorTab[1]=F_Color;
	
	zp->Canvas_ID=Canvas_ID;
	
	va_start(ap, fmt);
	DoZPrint( fmt, ap );
	va_end( ap );	
	
	///////str in zp_str, zp_len
	for(i=0; i<zp_len; i++){
		zp_data[i] = zp_str[i];
	}
	
	zp_data [zp_len]=0;
	
	TWLL_String_Create(0,zp);
	
}


//ZPrintf(StringShowOnCanvas[4].Character[0].Str_Lang,"zzzzzz");
//ZPrintf(StringShowOnCanvas[3].Character[0].Str_Lang,"%d",val);
#if 0
void ZPrintf(U16 * buf, const char *fmt, ...)
{
	int i;
	 BYTE *P;
	va_list ap;

	va_start(ap, fmt);
	DoZPrint( fmt, ap );
	va_end( ap );

	///////str in zp_str, zp_len
	P=zp_str;
	for(i=0; i<zp_len; i++){
		*buf= *P;
		buf++;
		P++;
	}
	
	*buf =0;
}
#endif
void ZPrintf(U16 Number,const char *fmt, ...)  //erek 2011-1-26 for bank overflow issue
{
	int i;
	 BYTE *P;
	 U16 * buf;
	va_list ap;

	va_start(ap, fmt);
	DoZPrint( fmt, ap );
	va_end( ap );

	buf=StringShowOnCanvas[Number].Character[OSDLang].Str_Lang;

	///////str in zp_str, zp_len
	P=zp_str;
	for(i=0; i<zp_len; i++){
		*buf= *P;
		buf++;
		P++;
	}
	
	*buf =0;
}


void ZPrintf_Unicode(U16 x, U16 y, U16 B_Color, U16 F_Color,U8 Canvas_ID, U16 UnicodeNum)
{
	U16 i;
	
	U16 StringColorTab[2]={0x0000,0xfffe};

	FONTCOLOR StringColor[]={
		StringColorTab,
		2,
	};
	
	FontControl_struct zp_control[]={
		{{120,20},0,StringColor},
	};
	
	U16 zp_data[2];

	CONST Character_ptr String_character[]={
	{zp_data },
	{zp_data },
	{zp_data },
	{zp_data },
	{zp_data },
	};
	
	UIControlString_struct zp[] = {{0, zp_control ,5, String_character,0, 0}};
	
	zp_control[0].XY1.X = x;
	zp_control[0].XY1.Y = y;

	StringColorTab[0]=B_Color;
	StringColorTab[1]=F_Color;
	
	zp->Canvas_ID=Canvas_ID;
	
	zp_data [0]=UnicodeNum;
	zp_data [1]=0;

	Use_UnicodeFont=1;
	
	TWLL_String_Create(0,zp);

	Use_UnicodeFont=0;
	
}

void ZPrintf_Unicode_8Bit(U16 x, U16 y, U8 B_Color, U8 F_Color,U8 Canvas_ID, U16 UnicodeNum)
{
	U16 i;
	
	U8 StringColorTab[2]={0x00,0x01};

	FONTCOLOR StringColor[]={
		StringColorTab,
		2,
	};
	
	FontControl_struct zp_control[]={
		{{120,20},0,StringColor},
	};
	
	U16 zp_data[2];

	CONST Character_ptr String_character[]={
	{zp_data },
	{zp_data },
	{zp_data },
	{zp_data },
	{zp_data },
	};
	
	UIControlString_struct zp[] = {{0, zp_control ,5, String_character,0, 0}};
	
	zp_control[0].XY1.X = x;
	zp_control[0].XY1.Y = y;

	StringColorTab[0]=B_Color;
	StringColorTab[1]=F_Color;
	
	zp->Canvas_ID=Canvas_ID;
	
	zp_data [0]=UnicodeNum;
	zp_data [1]=0;

	Use_UnicodeFont=1;
	
	TWLL_String_Create(0,zp);

	Use_UnicodeFont=0;
	
}



